home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / SPECTRAL.TST / PUTXFRAC.CX < prev    next >
Text File  |  1996-03-09  |  3KB  |  121 lines

  1. /* ============ */
  2. /* PutXFrac.cx    */
  3. /* ============ */
  4. #include <stdio.h>
  5. #include <xtendefs.h>
  6. #include <string.h>
  7. /* ========================================================= */
  8. /* fPutXFrac - Prints mixed fraction extended value on Unit   */
  9. /* ========================================================= */
  10. void
  11. fPutXFrac(FILE *Unit, USHORT * XFrac, short NFrac, short MinFld)
  12. {
  13.     static
  14.     USHORT  Eten[NE], InitDone = FALSE;
  15.     char    AscVal[128] = {0};
  16.     short   CharCt = 3, ExpVal;
  17.     char   *CharLoc;
  18.     short   i;
  19.     USHORT  MyXFrac[NE];
  20.  
  21.     if (!InitDone)
  22.     {
  23.     ASCTOX("10.0", Eten);
  24.     InitDone = TRUE;
  25.     }
  26.     XCOPY(XFrac, MyXFrac);
  27.     if (XGTE(MyXFrac, EZERO))
  28.     {                    /* Input is positive  */
  29.     fprintf(Unit, " ");        /* Put blank for sign */
  30.     }
  31.     else
  32.     {
  33.     XABS(MyXFrac);
  34.     fprintf(Unit, "-");        /* Put Negative sign */
  35.     }
  36.     for (i = 1; i <= NFrac; ++i)
  37.     {
  38.     XRMULT(MyXFrac, Eten);
  39.     }
  40.  
  41.     XROUND(MyXFrac, MyXFrac);        /* Get Desired Digits Only */
  42.     XTOASC(MyXFrac, AscVal, NDEC);    /* Convert to ASCII       */
  43.  
  44.     CharLoc = strrchr(AscVal, 'E');
  45.     *CharLoc = '\0';
  46.  
  47.     sscanf(CharLoc + 1, "%d", &ExpVal);    /* Get exponent */
  48.  
  49.     CharLoc = AscVal + 1;        /* Points past sign char */
  50.  
  51.     if (ExpVal < NFrac)
  52.     {
  53.     fprintf(Unit, "0.");        /* Minimum is three chars */
  54.     for (i = 1; i < NFrac - ExpVal ; ++i)
  55.     {
  56.         fprintf(Unit, "0");
  57.         ++CharCt;
  58.     }
  59.     fprintf(Unit, "%c", *CharLoc++);/* Print Second char */
  60.     ++CharLoc;            /* Third char is '.' */
  61.     for (; i < NFrac; ++i)
  62.     {
  63.         fprintf(Unit, "%c", *CharLoc++);
  64.         ++CharCt;
  65.     }
  66.     ++CharCt;
  67.     }
  68.     else
  69.     {
  70.     /* ----------------- */
  71.     /* Print Second char */
  72.     /* ----------------- */
  73.     fprintf(Unit, "%c", *CharLoc++);
  74.  
  75.     /* ----------------- */
  76.     /* Third char is '.' */
  77.     /* ----------------- */
  78.     ++CharLoc;
  79.  
  80.     for (i = NFrac; i < ExpVal; ++i)
  81.     {
  82.         if (*CharLoc)        /* Print next character */
  83.         {
  84.         fprintf(Unit, "%c", *CharLoc++);
  85.         }
  86.         else
  87.         {
  88.         fprintf(Unit, "0");    /* Pad with zeros on right */
  89.         }
  90.         ++CharCt;
  91.     }
  92.     fprintf(Unit, ".");        /* Insert decimal point */
  93.  
  94.     if (*CharLoc)
  95.     {
  96.         /* Append fraction */
  97.         for (i = 1; i <= NFrac; ++i)
  98.         {
  99.         fprintf(Unit, "%c", *CharLoc++);
  100.         ++CharCt;
  101.         }
  102.     }
  103.     }
  104.     /* ----------------------------- */
  105.     /* Fill field with blanks on the */
  106.     /* right to MinFld characters.   */
  107.     /* ----------------------------- */
  108.     for (; CharCt < MinFld; ++CharCt)
  109.     {
  110.     fprintf(Unit, " ");
  111.     }
  112. }
  113. /* ========================================================= */
  114. /* PutXFrac - Prints mixed fraction extended value on stdout */
  115. /* ========================================================= */
  116. void
  117. PutXFrac(USHORT * XFrac, short NFrac, short MinFld)
  118. {
  119.     fPutXFrac(stdout, XFrac, NFrac, MinFld);
  120. }
  121.